package com.javasm.sys.security.service;

import com.javasm.sys.SysUser;
import com.javasm.sys.UserService;
import com.javasm.sys.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginUserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.getOneByUname(username);
        if(user==null) throw new UsernameNotFoundException("用户名不存在");
        List<String> premiss= userService.listPremissionsByRoleId(user.getRoleId()+"");
        return new LoginUser(user,premiss);
    }
}
